//总金额
const allMoney = document.getElementById('totalmoney')  
//厨师代做列表
const cookList = document.getElementById('cooklist')
//厨师的状态
const cookerStatus = document.getElementById('cookerStuaus')
//顾客的菜单
const gukeMenu = document.getElementById('gukeMenu')
//顾客的状态
const gukeStatus = document.getElementById('gukeStatus')
//顾客队列
const gukeListg = document.getElementById('gukelist')
// 服务员的位置
const serverPositon = document.getElementById('serverPositon')
//单位时间
const timeUnit = 1000

//显示顾客的队列
function showGukeList(){
  let html= ''
  for(let i=0;i<queue.gukeArray.length;i++){
     if(!queue.gukeArray[i].jindian){
        html += `<li>${queue.gukeArray[i].name}</li>` 
     }
  }
  gukeListg.innerHTML  = html
}

//展示顾客的菜单
function showGukeMenuList(obj){
  let html = ''
  let textArr = ['×','√','△']
  for(let i=0;i<obj.g_menu.length;i++){
    let k = obj.g_menu[i].cai_status
    html += `<li>${obj.g_menu[i].name}&emsp;${textArr[k]}</li>`
  }
  gukeMenu.innerHTML = html
}

//展示厨师的菜单
function showCookerList(){
  let html = ''
  for(let i = 0;i<smallCooker.cookList.length;i++){
     if(smallCooker.cookList[i].cai_status<1){
        caitime = smallCooker.cookList[i].time
        html += `<li>${smallCooker.cookList[i].name}  ${caitime}s</li>`
     }else{
        html += `<li>${smallCooker.cookList[i].name}  √</li>`
     } 
  }
  cookList.innerHTML = html 
}

//顾客列表进店
function goSky(){
    if(queue.gukeArray.length>0){
        queue.gukeArray[0].jindian = true
        showGukeList()
        serverPositon.style.textAlign = 'right' //服务员位置
        gukeStatus.innerHTML = queue.gukeArray[0].name + '进店了,准备开始点菜'
        orderMenu(queue.gukeArray[0]).then(w_send_food).then(c_cook_menu)
    }else{
        console.log('一拨人吃完了')
    }
}

//点菜方法
function orderMenu(obj){
    return new Promise(function(reslove,reject){
      let diancaiTime = 4 //点菜3s钟时间倒计时
      obj.pointFood() //内部点菜方法
      // 点菜倒计时
      let Gt = setInterval(() => { //定时器刷新页面显示的顾客状态
        diancaiTime--
        gukeStatus.innerHTML = obj.name + '正在点菜，还差'+ diancaiTime + 's点好'
        //点好菜后的方法
        if(diancaiTime == 0){
          reslove(obj) //点好后的行为
          clearInterval(Gt) //清除定时器
          gukeStatus.innerHTML = obj.name + '点好菜了，正在等待上菜'
          showGukeMenuList(obj) //显示顾客点菜的列表
        }
      }, timeUnit)
      
    })  
}

//服务员告诉厨师点的菜单
function w_send_food(obj){
  return new Promise((reslove,reject)=>{
      console.log('服务员正在送菜')
      setTimeout(()=>{
        smallWaiter.tell(obj.g_menu,smallCooker)
        serverPositon.style.textAlign = 'left'
        reslove(obj)
      },timeUnit * 2)
  })
}

//厨师做菜
function c_cook_menu(obj){
  showCookerList()
  let caijiange = 0
  for(let i=0;i<smallCooker.cookList.length;i++){
      if(i==0){
        caijiange = 0
      }else{
        caijiange += smallCooker.cookList[i-1].time
      }
      setTimeout(()=>{
        let time = smallCooker.cookList[i].time
        let caidaoji = setInterval(()=>{
          cookerStatus.innerHTML = '厨师正在做' + smallCooker.cookList[i].name + '还差'+time+'s做好'
          time--
          showGukeMenuList(obj)
          console.log(obj)
          if(time == 0){
            smallCooker.cookList[i].cai_status = 1
            cookerStatus.innerHTML = '空闲'
            eatDish(obj)
            showCookerList()
            showGukeMenuList(obj)
            clearInterval(caidaoji)
           }
         },1000)
       },1000 * caijiange)
 
  }
  
}

//顾客吃饭
function eatDish(obj){
   let eatdingshiqi = setInterval(()=>{
    showGukeMenuList(obj)
   },1000)

   for(let i=0;i<obj.g_menu.length;i++){
      if(obj.g_menu[i].cai_status == 1){
        setTimeout(()=>{
          obj.g_menu[i].cai_status = 2
          let time = 3
          gukeStatus.innerHTML = '顾客正在吃' + obj.g_menu[i].name + time
      let a =    setInterval(()=>{
            time--
            gukeStatus.innerHTML = '顾客正在吃' + obj.g_menu[i].name + time
            if(time == 0){
              clearInterval(eatdingshiqi)
              clearInterval(a)
              if(  i == obj.g_menu.length - 1){
                gukeStatus.innerHTML = '全部吃完了准备结账'
                payMoney(obj)
              }
            
            }

          },1000)
        },i*2000)
       
      }
   }
}

// 顾客付钱
function payMoney(obj){

   for(let i=0;i<obj.g_menu.length;i++){
     skyPeople.money += obj.g_menu[i].price
   }
   queue.out()
   setTimeout(()=>{
    allMoney.innerHTML =  skyPeople.money
    gukeStatus.innerHTML = '结完账'
    goSky() 
    
   },3000)
   

}

goSky()